{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Challenge Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem: Given sorted arrays A, B, merge B into A in sorted order.\n",
    "\n",
    "* [Constraints](#Constraints)\n",
    "* [Test Cases](#Test-Cases)\n",
    "* [Algorithm](#Algorithm)\n",
    "* [Code](#Code)\n",
    "* [Unit Test](#Unit-Test)\n",
    "* [Solution Notebook](#Solution-Notebook)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Constraints\n",
    "\n",
    "* Does A have enough space for B?\n",
    "    * Yes\n",
    "* Can the inputs have duplicate array items?\n",
    "    * Yes\n",
    "* Can we assume the inputs are valid?\n",
    "    * No\n",
    "* Does the inputs also include the actual size of A and B?\n",
    "    * Yes\n",
    "* Can we assume this fits memory?\n",
    "    * Yes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test Cases\n",
    "\n",
    "* A or B is None -> Exception\n",
    "* index of last A or B < 0 -> Exception\n",
    "* A or B is empty\n",
    "* General case\n",
    "    * A = [1,  3,  5,  7,  9,  None,  None,  None]\n",
    "    * B = [4,  5,  6]\n",
    "    * A = [1, 3, 4, 5, 5, 6, 7, 9]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Algorithm\n",
    "\n",
    "Refer to the [Solution Notebook](http://nbviewer.jupyter.org/github/donnemartin/interactive-coding-challenges/blob/master/sorting_searching/merge_into/merge_into_solution.ipynb).  If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Array(object):\n",
    "\n",
    "    def merge_into(self, source, dest, source_end_index, dest_end_index):\n",
    "        # TODO: Implement me\n",
    "        pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unit Test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**The following unit test is expected to fail until you solve the challenge.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %load test_merge_into.py\n",
    "import unittest\n",
    "\n",
    "\n",
    "class TestArray(unittest.TestCase):\n",
    "\n",
    "    def test_merge_into(self):\n",
    "        array = Array()\n",
    "        self.assertRaises(TypeError, array.merge_into, None, None, None, None)\n",
    "        self.assertRaises(ValueError, array.merge_into, [1], [2], -1, -1)\n",
    "        a = [1, 2, 3]\n",
    "        self.assertEqual(array.merge_into(a, [], len(a), 0), [1, 2, 3])\n",
    "        a = [1, 2, 3]\n",
    "        self.assertEqual(array.merge_into(a, [], len(a), 0), [1, 2, 3])\n",
    "        a = [1,  3,  5,  7,  9,  None,  None,  None]\n",
    "        b = [4,  5,  6]\n",
    "        expected = [1, 3, 4, 5, 5, 6, 7, 9]\n",
    "        self.assertEqual(array.merge_into(a, b, 5, len(b)), expected)\n",
    "        print('Success: test_merge_into')\n",
    "\n",
    "\n",
    "def main():\n",
    "    test = TestArray()\n",
    "    test.test_merge_into()\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Solution Notebook\n",
    "\n",
    "Review the [Solution Notebook]() for a discussion on algorithms and code solutions."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
